package com.ctshk.app.train.convertor;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.individual.conversion.*;
import com.ctshk.common.utils.individual.conversion.dto.*;
import com.ctshk.rpc.train.dto.*;
import com.ctshk.rpc.train.req.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *
 * </p>
 *
 * @author 彭柳霖
 * @since 2021/3/16
 */
public class Convertor {

    /**
     * 前端统一结构转成火车产品结构，l2 to l1
     *
     * @param batchStrategyReq
     * @return
     */
    public static TrainProductStrategySaveBatchReq frontToTrainProduct(BatchStrategyReq batchStrategyReq) {
        TrainProductStrategySaveBatchReq trainProductStrategySaveBatchReq = new TrainProductStrategySaveBatchReq();
        // 获取巴士产品ID列表
        trainProductStrategySaveBatchReq.setTrainProductIdList(batchStrategyReq.getProductIds());
        // 获取策略列表
        List<StrategyReq> batchStrategyReqStrategyList = batchStrategyReq.getStrategyList();
        List<TrainProductStrategyReq> trainProductStrategyReqList = new ArrayList<>();
        trainProductStrategySaveBatchReq.setTrainProductStrategyReqList(trainProductStrategyReqList);
        for (StrategyReq strategyReq : batchStrategyReqStrategyList) {
            TrainProductStrategyReq trainProductStrategyReq = new TrainProductStrategyReq();
            trainProductStrategyReqList.add(trainProductStrategyReq);
            //策略主表
            trainProductStrategyReq.setIncreaseStrategyName(strategyReq.getName());

            //策略时间
            List<ProductStrategyDateReq> productStrategyDateReqList = strategyReq.getTime();
            List<TrainProductStrategyDateReq> trainProductStrategyDateReqList = EntityUtil.copyList(productStrategyDateReqList, TrainProductStrategyDateReq.class);

            trainProductStrategyReq.setTrainProductStrategyDateReqList(trainProductStrategyDateReqList);
            //渠道加幅规则
            ChannelSaleReq channelSaleReq = strategyReq.getChannelSaleReq();
            //渠道加幅规则-线下自营渠道
            ProductOfflineChannelReq productOfflineChannelReq = channelSaleReq.getProductOfflineChannelReq();
            if(!Objects.isNull(productOfflineChannelReq)){
                trainProductStrategyReq.setSelfSalePermissionType(productOfflineChannelReq.getPermissionType());
                if (trainProductStrategyReq.getSelfSalePermissionType() != null) {
                    if (trainProductStrategyReq.getSelfSalePermissionType() == 2) {
                        TrainProductIncreaseStrategyRuleReq trainProductIncreaseStrategyRule = EntityUtil.copy(productOfflineChannelReq, TrainProductIncreaseStrategyRuleReq.class);
                        trainProductStrategyReq.setTrainProductIncreaseStrategySelfRule(trainProductIncreaseStrategyRule);
                    }
                    if (trainProductStrategyReq.getSelfSalePermissionType() == 3) {
                        List<TrainProductChannelOfflineSelfCustomReq> trainProductChannelOfflineSelfCustomReqList = new ArrayList<>();

                        List<ProductChannelOfflineSelfCustomReq> productChannelOfflineSelfCustomReqList = productOfflineChannelReq.getProductChannelOfflineSelfCustomReqList();

                        for (ProductChannelOfflineSelfCustomReq productChannelOfflineSelfCustomReq : productChannelOfflineSelfCustomReqList) {
                            TrainProductChannelOfflineSelfCustomReq trainProductChannelOfflineSelfCustomReq = new TrainProductChannelOfflineSelfCustomReq();
                            trainProductChannelOfflineSelfCustomReq.setName(productChannelOfflineSelfCustomReq.getName());
                            trainProductChannelOfflineSelfCustomReq.setCheckType(productChannelOfflineSelfCustomReq.getCheckType());
                            trainProductChannelOfflineSelfCustomReq.setRelationId(productChannelOfflineSelfCustomReq.getRelationId());

                            TrainProductIncreaseStrategyRuleReq trainProductIncreaseStrategyRule = EntityUtil.copy(productChannelOfflineSelfCustomReq, TrainProductIncreaseStrategyRuleReq.class);
                            trainProductChannelOfflineSelfCustomReq.setTrainProductIncreaseStrategyRuleReq(trainProductIncreaseStrategyRule);
                            trainProductChannelOfflineSelfCustomReqList.add(trainProductChannelOfflineSelfCustomReq);
                        }
                        trainProductStrategyReq.setTrainProductChannelOfflineSelfCustomReqList(trainProductChannelOfflineSelfCustomReqList);
                    }
                }
            }



            //渠道加幅规则-线下代理商渠道
            ProductAgentReq productAgentReq = channelSaleReq.getProductAgentReq();
            if(!Objects.isNull(productAgentReq)){
                trainProductStrategyReq.setAgentSalePermissionType(productAgentReq.getPermissionType());
                if (trainProductStrategyReq.getAgentSalePermissionType() != null) {
                    if (trainProductStrategyReq.getAgentSalePermissionType() == 2) {
                        TrainProductIncreaseStrategyRuleReq trainProductIncreaseStrategyRule = EntityUtil.copy(productAgentReq, TrainProductIncreaseStrategyRuleReq.class);
                        trainProductStrategyReq.setTrainProductIncreaseStrategyAgentRule(trainProductIncreaseStrategyRule);
                    }
                    if (trainProductStrategyReq.getAgentSalePermissionType() == 3) {
                        List<TrainProductChannelOfflineAgentCustomReq> trainProductChannelOfflineAgentCustomReqs = new ArrayList<>();

                        List<ProductChannelOfflineAgentCustomReq> productChannelOfflineAgentCustomReqList = productAgentReq.getProductChannelOfflineAgentCustomReqList();
                        for (ProductChannelOfflineAgentCustomReq productChannelOfflineAgentCustomReq : productChannelOfflineAgentCustomReqList) {
                            TrainProductChannelOfflineAgentCustomReq trainProductChannelOfflineAgentCustomReq = new TrainProductChannelOfflineAgentCustomReq();
                            trainProductChannelOfflineAgentCustomReq.setAgentId(productChannelOfflineAgentCustomReq.getAgentId());
                            trainProductChannelOfflineAgentCustomReq.setAgentName(productChannelOfflineAgentCustomReq.getAgentName());

                            TrainProductIncreaseStrategyRuleReq trainProductIncreaseStrategyRule = EntityUtil.copy(productChannelOfflineAgentCustomReq, TrainProductIncreaseStrategyRuleReq.class);
                            trainProductChannelOfflineAgentCustomReq.setTrainProductIncreaseStrategyRuleReq(trainProductIncreaseStrategyRule);
                            trainProductChannelOfflineAgentCustomReqs.add(trainProductChannelOfflineAgentCustomReq);

                        }
                        trainProductStrategyReq.setTrainProductChannelOfflineAgentCustomReqList(trainProductChannelOfflineAgentCustomReqs);
                    }
                }
            }



            //渠道加幅规则-线上自营渠道官方平台
            List<ProductChannelOnlineSelfPlatformReq> productChannelOnlineSelfPlatformReqList = channelSaleReq.getProductChannelOnlineSelfPlatformReqList();
            if (!org.springframework.util.CollectionUtils.isEmpty(productChannelOnlineSelfPlatformReqList)) {
                List<TrainProductChannelOnlineSelfPlatformReq> trainProductChannelOnlineSelfPlatformReqList = new ArrayList<>();
                TrainProductChannelOnlineSelfPlatformReq trainProductChannelOnlineSelfPlatformReq = null;
                for (ProductChannelOnlineSelfPlatformReq productChannelOnlineSelfPlatformReq : productChannelOnlineSelfPlatformReqList) {
                    trainProductChannelOnlineSelfPlatformReq = new TrainProductChannelOnlineSelfPlatformReq();
                    //渠道信息
                    trainProductChannelOnlineSelfPlatformReq.setChannelId(productChannelOnlineSelfPlatformReq.getChannelId());
                    trainProductChannelOnlineSelfPlatformReq.setChannelName(productChannelOnlineSelfPlatformReq.getChannelName());

                    TrainProductIncreaseStrategyRuleReq trainProductIncreaseStrategyRule = EntityUtil.copy(productChannelOnlineSelfPlatformReq, TrainProductIncreaseStrategyRuleReq.class);
                    trainProductChannelOnlineSelfPlatformReq.setTrainProductIncreaseStrategyRuleReq(trainProductIncreaseStrategyRule);
                    trainProductChannelOnlineSelfPlatformReqList.add(trainProductChannelOnlineSelfPlatformReq);
                }
                trainProductStrategyReq.setTrainProductChannelOnlineSelfPlatformReqList(trainProductChannelOnlineSelfPlatformReqList);
            }






            //渠道加幅规则-线上第三方平台
            List<ProductChannelOnlineThirdPlatformReq> productChannelOnlineThirdPlatformReqList = channelSaleReq.getProductChannelOnlineThirdPlatformReqList();
            if (CollectionUtils.isNotEmpty(productChannelOnlineThirdPlatformReqList)) {
                List<TrainProductChannelOnlineThirdPlatformReq> trainProductChannelOnlineThirdPlatformReqList = new ArrayList<>();
                TrainProductChannelOnlineThirdPlatformReq trainProductChannelOnlineThirdPlatformReq = null;
                for (ProductChannelOnlineThirdPlatformReq productChannelOnlineThirdPlatformReq : productChannelOnlineThirdPlatformReqList) {
                    trainProductChannelOnlineThirdPlatformReq = new TrainProductChannelOnlineThirdPlatformReq();
                    //渠道信息
                    trainProductChannelOnlineThirdPlatformReq.setPlatformId(productChannelOnlineThirdPlatformReq.getPlatformId());
                    trainProductChannelOnlineThirdPlatformReq.setPlatformName(productChannelOnlineThirdPlatformReq.getPlatformName());


                    TrainProductIncreaseStrategyRuleReq trainProductIncreaseStrategyRule = EntityUtil.copy(productChannelOnlineThirdPlatformReq, TrainProductIncreaseStrategyRuleReq.class);
                    trainProductChannelOnlineThirdPlatformReq.setTrainProductIncreaseStrategyRuleReq(trainProductIncreaseStrategyRule);
                    trainProductChannelOnlineThirdPlatformReqList.add(trainProductChannelOnlineThirdPlatformReq);
                }
                trainProductStrategyReq.setTrainProductChannelOnlineThirdPlatformReqList(trainProductChannelOnlineThirdPlatformReqList);
            }
        }
        return trainProductStrategySaveBatchReq;
    }


    /**
     * 火车票产品结构转前端结构，l1 to l2
     *
     * @param trainProductStrategyDTOList
     * @return
     */
    public static BatchStrategyDto TrainProductToFront(List<TrainProductStrategyDTO> trainProductStrategyDTOList){

        if(CollectionUtils.isEmpty(trainProductStrategyDTOList)){
            return null;
        }
        BatchStrategyDto batchStrategyDto = new BatchStrategyDto();
        List<Long> productIds = new ArrayList<>();
        productIds.add(trainProductStrategyDTOList.get(0).getTrainProductId());

        List<StrategyDto> strategyList = new ArrayList<>();
        for(TrainProductStrategyDTO trainProductStrategyDTO : trainProductStrategyDTOList){
            StrategyDto strategyDto = new StrategyDto();
            strategyDto.setName(trainProductStrategyDTO.getIncreaseStrategyName());
            List<ProductStrategyDateReq> productStrategyDateReqList = EntityUtil.copyList(trainProductStrategyDTO.getTrainProductStrategyDateDTOList(), ProductStrategyDateReq.class);
            strategyDto.setTime(productStrategyDateReqList);
            ChannelSaleDto channelSaleDto = new ChannelSaleDto();

            //线下代理
            ProductAgentDto productAgentDto = new  ProductAgentDto();
            List<ProductChannelOfflineAgentCustomDto> productChannelOfflineAgentCustomDtoList = new ArrayList<>();
            if(!Objects.isNull(trainProductStrategyDTO.getTrainIncreaseStrategyAgentRule())){
                productAgentDto = EntityUtil.copy(trainProductStrategyDTO.getTrainIncreaseStrategyAgentRule(),ProductAgentDto.class);
            }
            List<TrainProductChannelOfflineAgentCustomDTO> trainProductChannelOfflineAgentCustomDTOList = trainProductStrategyDTO.getTrainProductChannelOfflineAgentCustomDTOList();
            if(!CollectionUtils.isEmpty(trainProductChannelOfflineAgentCustomDTOList)){
                for(TrainProductChannelOfflineAgentCustomDTO trainProductChannelOfflineAgentCustomDTO : trainProductChannelOfflineAgentCustomDTOList){
                    ProductChannelOfflineAgentCustomDto offlineAgentCustomDto = new ProductChannelOfflineAgentCustomDto();
                    TrainProductIncreaseStrategyRuleDTO trainProductIncreaseStrategyRuleDTO = trainProductChannelOfflineAgentCustomDTO.getTrainProductIncreaseStrategyRuleDTO();
                    offlineAgentCustomDto = EntityUtil.copy(trainProductIncreaseStrategyRuleDTO,ProductChannelOfflineAgentCustomDto.class);
                    offlineAgentCustomDto.setAgentId(trainProductChannelOfflineAgentCustomDTO.getAgentId());
                    offlineAgentCustomDto.setAgentName(trainProductChannelOfflineAgentCustomDTO.getAgentName());
                    productChannelOfflineAgentCustomDtoList.add(offlineAgentCustomDto);
                }
                productAgentDto.setProductChannelOfflineAgentCustomReqList(productChannelOfflineAgentCustomDtoList);
            }
            productAgentDto.setPermissionType(trainProductStrategyDTO.getAgentSalePermissionType());
            channelSaleDto.setProductAgentReq(productAgentDto);

            //线下自营
            ProductOfflineChannelDto productOfflineChannelDto = new ProductOfflineChannelDto();
            List<ProductChannelOfflineSelfCustomDto> productChannelOfflineSelfCustomDtoList = new ArrayList<>();
            if(!Objects.isNull(trainProductStrategyDTO.getTrainIncreaseStrategySelfRule())){
                productOfflineChannelDto = EntityUtil.copy(trainProductStrategyDTO.getTrainIncreaseStrategySelfRule(),ProductOfflineChannelDto.class);
            }
            List<TrainProductChannelOfflineSelfCustomDTO> trainProductChannelOfflineSelfCustomDTOList = trainProductStrategyDTO.getTrainProductChannelOfflineSelfCustomDTOList();
            if(!CollectionUtils.isEmpty(trainProductChannelOfflineSelfCustomDTOList)){
                for(TrainProductChannelOfflineSelfCustomDTO trainProductChannelOfflineSelfCustomDTO : trainProductChannelOfflineSelfCustomDTOList){
                    ProductChannelOfflineSelfCustomDto offlineSelfCustomDto = new ProductChannelOfflineSelfCustomDto();
                    TrainProductIncreaseStrategyRuleDTO trainProductIncreaseStrategyRuleDTO = trainProductChannelOfflineSelfCustomDTO.getTrainProductIncreaseStrategyRuleDTO();
                    offlineSelfCustomDto = EntityUtil.copy(trainProductIncreaseStrategyRuleDTO,ProductChannelOfflineSelfCustomDto.class);
                    offlineSelfCustomDto.setRelationId(trainProductChannelOfflineSelfCustomDTO.getRelationId());
                    offlineSelfCustomDto.setName(trainProductChannelOfflineSelfCustomDTO.getName());
                    productChannelOfflineSelfCustomDtoList.add(offlineSelfCustomDto);
                }
                productOfflineChannelDto.setProductChannelOfflineSelfCustomReqList(productChannelOfflineSelfCustomDtoList);
            }
            productOfflineChannelDto.setPermissionType(trainProductStrategyDTO.getSelfSalePermissionType());
            channelSaleDto.setProductOfflineChannelReq(productOfflineChannelDto);

            //线上自营
            List<ProductChannelOnlineSelfPlatformDto> productChannelOnlineSelfPlatformDtoList = new ArrayList<>();
            List<TrainProductChannelOnlineSelfPlatformDTO> trainProductChannelOnlineSelfPlatformDTOList = trainProductStrategyDTO.getTrainProductChannelOnlineSelfPlatformDTOList();
            if(!CollectionUtils.isEmpty(trainProductChannelOnlineSelfPlatformDTOList)){
                for(TrainProductChannelOnlineSelfPlatformDTO trainProductChannelOnlineSelfPlatformDTO : trainProductChannelOnlineSelfPlatformDTOList){
                    ProductChannelOnlineSelfPlatformDto productChannelOnlineSelfPlatformDto = new ProductChannelOnlineSelfPlatformDto();
                    TrainProductIncreaseStrategyRuleDTO trainProductIncreaseStrategyRuleDTO = trainProductChannelOnlineSelfPlatformDTO.getTrainProductIncreaseStrategyRuleDTO();
                    productChannelOnlineSelfPlatformDto = EntityUtil.copy(trainProductIncreaseStrategyRuleDTO,ProductChannelOnlineSelfPlatformDto.class);
                    productChannelOnlineSelfPlatformDto.setChannelId(trainProductChannelOnlineSelfPlatformDTO.getChannelId());
                    productChannelOnlineSelfPlatformDto.setChannelName(trainProductChannelOnlineSelfPlatformDTO.getChannelName());
                    productChannelOnlineSelfPlatformDtoList.add(productChannelOnlineSelfPlatformDto);
                }
                channelSaleDto.setProductChannelOnlineSelfPlatformReqList(productChannelOnlineSelfPlatformDtoList);
            }

            //线上第三方
            List<ProductChannelOnlineThirdPlatformDto> productChannelOnlineThirdPlatformDtoList = new ArrayList<>();
            List<TrainProductChannelOnlineThirdPlatformDTO> trainProductChannelOnlineThirdPlatformDTOList = trainProductStrategyDTO.getTrainProductChannelOnlineThirdPlatformDTOList();
            if(!CollectionUtils.isEmpty(trainProductChannelOnlineThirdPlatformDTOList)){
                for(TrainProductChannelOnlineThirdPlatformDTO trainProductChannelOnlineThirdPlatformDTO : trainProductChannelOnlineThirdPlatformDTOList){
                    ProductChannelOnlineThirdPlatformDto productChannelOnlineThirdPlatformDto = new ProductChannelOnlineThirdPlatformDto();
                    TrainProductIncreaseStrategyRuleDTO trainProductIncreaseStrategyRuleDTO = trainProductChannelOnlineThirdPlatformDTO.getTrainProductIncreaseStrategyRuleDTO();
                    productChannelOnlineThirdPlatformDto = EntityUtil.copy(trainProductIncreaseStrategyRuleDTO,ProductChannelOnlineThirdPlatformDto.class);
                    productChannelOnlineThirdPlatformDto.setPlatformId(trainProductChannelOnlineThirdPlatformDTO.getPlatformId());
                    productChannelOnlineThirdPlatformDto.setPlatformName(trainProductChannelOnlineThirdPlatformDTO.getPlatformName());
                    productChannelOnlineThirdPlatformDtoList.add(productChannelOnlineThirdPlatformDto);
                }
                channelSaleDto.setProductChannelOnlineThirdPlatformReqList(productChannelOnlineThirdPlatformDtoList);
            }
            strategyDto.setChannelSaleReq(channelSaleDto);
            strategyList.add(strategyDto);

        }
        batchStrategyDto.setProductIds(productIds);
        batchStrategyDto.setStrategyList(strategyList);
        return batchStrategyDto;

    }

}
